package com.ctrl.riveradministrator.mvp.view.weight;

import android.app.Dialog;
import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SearchView;
import android.widget.TextView;

import com.ctrl.riveradministrator.R;
import com.ctrl.riveradministrator.mvp.view.activity.trace.TraceActivity;

import java.util.List;

public class BottomDialog extends Dialog implements View.OnClickListener, AdapterView.OnItemClickListener , SearchView.OnQueryTextListener {

    private TextView titleTv, t1, t2, t3;
    private ConstraintLayout riverInfoCl, riverPolicyCl, riverDocCl, riverEngineeringCl;
    private ConstraintLayout info, report, traceDraw;
    public ListView list;
    private Context context;
    private BottomDiaItemOnClickListener listener;
    private BottomListItemOnClickListener listItemOnClickListener;
    private List<String> data;

    private int index = 0;//为不同的菜单添加点击事件索引
    public ArrayAdapter<String> mAdapter;
    public ProgressBar mPb_progress;

    public BottomDialog(Context context) {
        super(context, R.style.ActionSheetDialogStyle);
        this.context = context;
    }

    public BottomDialog(Context context,List<String> reachNames) {
        super(context, R.style.ActionSheetDialogStyle);
        this.context = context;
        this.data = reachNames;
    }


    public void setListener(BottomDiaItemOnClickListener listener) {
        this.listener = listener;
    }

    public void setListItemOnClickListener(BottomListItemOnClickListener listItemOnClickListener) {
        this.listItemOnClickListener = listItemOnClickListener;
    }


    public void showBottomRiverInfoDialog(String title) {
        index = 0;
        View view = LayoutInflater.from(context).inflate(R.layout.bottom_dialog, null);
        titleTv = view.findViewById(R.id.title_tv);
        riverInfoCl = view.findViewById(R.id.river_info_cl);
        riverPolicyCl = view.findViewById(R.id.river_policy_cl);
        riverDocCl = view.findViewById(R.id.river_doc_cl);
        riverEngineeringCl = view.findViewById(R.id.river_engineering_cl);
        riverInfoCl.setOnClickListener(this);
        riverPolicyCl.setOnClickListener(this);
        riverDocCl.setOnClickListener(this);
        riverEngineeringCl.setOnClickListener(this);
        titleTv.setText(title);
        setContentView(view);
        Window dialogWin = getWindow();
        dialogWin.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams layoutParams = dialogWin.getAttributes();
        layoutParams.y = 0;
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        dialogWin.setAttributes(layoutParams);
        show();
    }

    public void showBottomTraceSearchDialog(String title) {
        index = 1;
        View view = LayoutInflater.from(context).inflate(R.layout.bottom_trace_search_dialog, null);
        titleTv = view.findViewById(R.id.title_tv);
        info = view.findViewById(R.id.search_info_cl);
        report = view.findViewById(R.id.river_up_cl);
        traceDraw = view.findViewById(R.id.river_search_cl);
        info.setOnClickListener(this);
        report.setOnClickListener(this);
        traceDraw.setOnClickListener(this);
        titleTv.setText(title);
        setContentView(view);
        Window dialogWin = getWindow();
        dialogWin.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams layoutParams = dialogWin.getAttributes();
        layoutParams.y = 0;
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        dialogWin.setAttributes(layoutParams);
        show();
    }

    public void showReachDialog() {
        View view = LayoutInflater.from(context).inflate(R.layout.bottom_dialog_reach, null);
        SearchView searchView = view.findViewById(R.id.sView);
        mPb_progress = view.findViewById(R.id.pb_progress);
        list = view.findViewById(R.id.list);
        searchView.setOnQueryTextListener(this);
        searchView.setSubmitButtonEnabled(true);
        searchView.setQueryHint("搜索河道");
        list.setTextFilterEnabled(true);
        mAdapter = new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, data);
        list.setAdapter(mAdapter);
        list.setOnItemClickListener(this);
        setContentView(view);
        Window dialogWin = getWindow();
        dialogWin.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams layoutParams = dialogWin.getAttributes();
        layoutParams.y = 0;
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        dialogWin.setAttributes(layoutParams);
        show();
    }


    @Override
    public void onClick(View v) {
        switch (index) {
            case 0:
                switch (v.getId()) {
                    case R.id.river_info_cl:
                        listener.bottomDiaItem(0);
                        break;
                    case R.id.river_policy_cl:
                        listener.bottomDiaItem(1);
                        break;
                    case R.id.river_doc_cl:
                        listener.bottomDiaItem(2);
                        break;
                    case R.id.river_engineering_cl:
                        listener.bottomDiaItem(3);
                        break;
                }
                break;
            case 1:
                switch (v.getId()) {
                    case R.id.search_info_cl:
                        listener.bottomDiaItem(0);
                        break;
                    case R.id.river_up_cl:
                        listener.bottomDiaItem(1);
                        break;
                    case R.id.river_search_cl:
                        listener.bottomDiaItem(2);
                        break;
                }
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String item = mAdapter.getItem(position);
        Log.i("cyc", "item====" + item);
        listItemOnClickListener.bottomListDiaItem(item);
    }

    /**
     * Called when the user submits the query. This could be due to a key press on the
     * keyboard or due to pressing a submit button.
     * The listener can override the standard behavior by returning true
     * to indicate that it has handled the submit request. Otherwise return false to
     * let the SearchView handle the submission by launching any associated intent.
     *
     * @param query the query text that is to be submitted
     * @return true if the query has been handled by the listener, false to let the
     * SearchView perform the default action.
     */
    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    /**
     * Called when the query text is changed by the user.
     *
     * @param newText the new content of the query text field.
     * @return false if the SearchView should perform the default action of showing any
     * suggestions if available, true if the action was handled by the listener.
     */
    @Override
    public boolean onQueryTextChange(String newText) {
        Log.i("cyc","newText==="+newText);
        if (TextUtils.isEmpty(newText)) {
            list.clearTextFilter();// 清除listview的过滤
        } else {
            list.setFilterText(newText);// 设置过滤词组
        }
        return true;
    }

    public interface BottomDiaItemOnClickListener {
        void bottomDiaItem(int is);
    }

    public interface BottomListItemOnClickListener {
        void bottomListDiaItem(String item);
    }
}
